//登录
var login;
//注册
var register;
//注册表单
var registerForm;
//状态
var state=0;

$(function(){
 login=$('#login').dialog({
			title:'用户登录',
			width: 250,   
		    height: 250,   
		    cache: false,
		    closable:false,
		    modal:true,
		    buttons:[{
		    	 text:'注册',
		    	 handler:function(){
		    		 if(state=0){
		    			 state=1;
		    		 }
		    		 //每次切换住注册页面，隐藏窗口，清除数据
		    		 $('#codevalidateRegister').click(changeRegister($('#codevalidateRegister')[0]));
		    		 $('#loginForm input').val('');
		    		 login.dialog('close');
		    		 register.dialog('open');
		         }
		      },
		      {
			       text:'登录',
			       handler:function(){
			    	   if($('#loginForm').form('validate')){
			    		   $.ajax({
				    		   url:projectName+'/login.do',
				    		   type:'post',
				    		   data:{
				    			   userName:$('#loginForm input[name=name]').val(),
				    			   userPassword:$('#loginForm input[name=password]').val(),
				    			   codevalidates:$('#loginForm input[name=codevalidates]').val(),
				    		   },
				    		   dataType:'json',
				    		   success:function(log){
				    			   if(log.status=="success"){
				    				   window.location.href=projectName+"/home.do";
				    			   }else{
				    				   $.messager.alert('提示',log.message);
				    				   //重新刷验证码
				    				   $('#codevalidateLogin').click(changeRegister($('#codevalidateLogin')[0]));
				    				   $('#loginForm input[name=codevalidates]').val('');
				    			   }
				    		   },
			    	   	});
			       	  }

			        }
			    }
		    ]
		});
 
 register=$('#register').show().dialog({
		title:'用户注册',
		width: 250,   
	    height: 250,   
	    cache: false,
	    closable:false,
	    modal:true,
	    closed:true,
	    buttons:[{
	    	 text:'返回',
	    	 handler:function(){
	    		 //每次切换住注册页面，隐藏窗口 清除数据
	    		 $('#codevalidateLogin').click(changeLogin( $('#codevalidateLogin')[0]));
	    		 $('#registerForm input').val('');
	    		 register.dialog('close');
	    		 login.dialog('open');
	         }
	      },
	      {
		       text:'注册',
		       handler:function(){
		    	   if($('#registerForm').form('validate')){
		    		   if($('#registerForm input[name=password]').val()!=$('#registerForm input[name=passwordRepetition]').val()){
		    			      $.messager.alert('提示','密码不一致!');
		    			      $('#registerForm input[name=codevalidates]').val('');
		    			      //重新刷验证码
		    			      return $('#codevalidateRegister').click(changeRegister($('#codevalidateRegister')[0]));
		    		   }
		    		   $.ajax({
			    		   url:projectName+'/register.do',
			    		   type:'post',
			    		   data:{
			    			   userName:$('#registerForm input[name=name]').val(),
			    			   userPassword:$('#registerForm input[name=password]').val(),
			    			   codevalidates:$('#registerForm input[name=codevalidates]').val(),
			    		   },
			    		   dataType:'json',
			    		   success:function(log){
			    			   if(log.status=="success"){
			    				   $.messager.alert('提示','注册成功','info',function(){
			    					   window.location.reload();
			    				   });
			    			   }else{
			    				   $.messager.alert('提示',log.message);
			    				   $('#codevalidateRegister').click(changeRegister($('#codevalidateRegister')[0]));
			    				   $('#registerForm input[name=codevalidates]').val('');
			    			   }
			    		   },
		    	   	});
		       	  }
		        }
		    }
	    ]
	});
 
 
});


//验证码
function changeLogin(img){
	console.log(img+"--------------------1");
	 img.src='/appraisal/code/'+new Date().getTime()+".do";
}


//验证码
function changeRegister(img){
	if(state=0){
		$('#codevalidateRegister').attr('src','');
	}else{
		img.src='/appraisal/code/'+new Date().getTime()+".do";
	}
}
